#!/user/bin/python3
# @Author:  LSY
# @Date:    2020/11/8
from app.models.bo.hanfan.hanfan_model_input_bo import HanFanModelInputBO
from app.services.models.pickle_model import PickleModel
from flask import current_app
import pandas as pd
import numpy as np
from app.utils.constant import HanFanStatus


class HanFanScoreModel(PickleModel):

    def __init__(self):
        super().__init__(model_name="hanfan_score_model")

    def process(self, hanfan_model_input_bo):
        try:
            params_dict = {}
            for key, value in hanfan_model_input_bo.__dict__.items():
                tlist = []
                if value is None:
                    value = HanFanStatus.YUAN_RAN_LIAO_DEFAULT_VALUE
                tlist.append(value)
                params_dict[key] = tlist

            input_df = pd.DataFrame.from_dict(params_dict)
            input_df = input_df.loc[0:1, ['CG_LT_GL_GL04_Tie_V1',
                                          'CG_LT_GL_GL04_LQBPSZGLLFET1530',
                                          'CG_LT_GL_GL04_Tie_S5',
                                          'CG_LT_GL_GL04_COZXFX',
                                          'CG_LT_GL_GL04_19507WD180270XN2',
                                          'CG_LT_GL_GL04_Yuanranliao_Qiu_S',
                                          ]]
            score = self.model_instance.predict(input_df)
            return score[0].astype(np.float)
        except Exception as e:
            current_app.logger.info(e)

    def simple_process(self, input: HanFanModelInputBO):
        a = self.get_default(input.CG_LT_GL_GL04_Tie_V1)
        b = self.get_default(input.CG_LT_GL_GL04_Tie_Mn1)

        c = self.get_default(input.CG_LT_GL_GL04_Tie_Ni1)
        d = self.get_default(input.CG_LT_GL_GL04_Tie_Mo1)
        e = self.get_default(input.CG_LT_GL_GL04_LDWDBG5700TE1106)
        f = self.get_default(input.CG_LT_GL_GL04_LDWDBG5700TE1117)
        g = self.get_default(input.CG_LT_GL_GL04_LGWDBG6500TE1157)
        h = self.get_default(input.CG_LT_GL_GL04_Tie_S1)

        y = 0.04929 + a*0.805 - b*0.010296 + c*0.33072 + d*0.131596 + e*0.000335 - f*0.000001 - g*0.000039 - h*0.030365
        return y

    def get_default(self, value):
        if value is None: return 0
        return value


hanfan_score_model = HanFanScoreModel()
